//
//  NSDate+GGExtension.m
//  ggtool
//
//  Created by engfei on 2017/4/27.
//  Copyright © 2017年 GaiGai. All rights reserved.
//

#import "NSDate+GGExtension.h"

@implementation NSDate (GGExtension)

- (NSUInteger)numberOfDaysInCurrentMonth {
    return [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self].length;
}

// 一个月总共有几周
- (NSUInteger)numberOfWeeksInCurrentMonth {
    // 当前月的第一天是星期几
    NSUInteger firstWeekDay = [[self firstDayOfCurrentMonth] weeklyOrdinality]; // 7
    
    NSUInteger monthDays = [self numberOfDaysInCurrentMonth]; // 30
    NSUInteger weeks = 0;
    
    if (firstWeekDay > 1) {
        weeks += 1;
        monthDays -= (7 - firstWeekDay + 1);
    }
    
    weeks += monthDays / 7;
    weeks += (monthDays % 7 > 0) ? 1: 0;
    
    return weeks;
    
}

// 当前是星期几
- (NSUInteger)weeklyOrdinality {
    return [[NSCalendar currentCalendar] ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfMonth forDate:self];
}

// 当前是几号
- (NSUInteger)monthlyOrdinality {
    return [[NSCalendar currentCalendar] ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self];
}

// 月初第一天的NSDate对象
- (NSDate *)firstDayOfCurrentMonth {
    NSDate *startDate = nil;
    BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMonth startDate:&startDate interval:NULL forDate:self];
    NSAssert1(ok, @"firstdayofcurrentmonth = %@", self);
    return startDate;
}

// 当前月最后一天的NSDate对象
- (NSDate *)lastDayOfCurrentMonth {
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:unit fromDate:self];
    dateComponents.day = [self numberOfDaysInCurrentMonth];
    return [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
}

// 上一月
- (NSDate *)dayInThePreviousMonth {
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.month = -1;
    return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self options:0];
}

// 下一月
- (NSDate *)dayInTheFollowingMonth {
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.month = 1;
    return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self options:0];
    
}

// 当前的年月日时间元素
- (NSDateComponents *)YMDComponents {
    
    return  [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
}

// 当前是第几周,从0开始算起
- (NSUInteger)weekNumberInCurrentMonth {
    
    // 当前月的第一天是星期几
    NSUInteger firstDay = [[self firstDayOfCurrentMonth] weeklyOrdinality];
    // 当前月有几周
    NSUInteger weeksCount = [self numberOfWeeksInCurrentMonth];
    NSUInteger weekNumber = 0;
    
    NSDateComponents *c = [self YMDComponents];
    NSUInteger startIndex = [[self firstDayOfCurrentMonth] monthlyOrdinality];
    NSUInteger endIndex = startIndex + (7 - firstDay);
    
    for (int i = 0; i < weeksCount; i++) {
        if (c.day >= startIndex && c.day <= endIndex) {
            weekNumber = i;
            break;
        }
        startIndex = endIndex + 1;
        endIndex = startIndex + 6;
    }

    return weekNumber;
}


// 相对于当前月偏移量为offset的总天数
- (NSUInteger)numberOfDaysInCurrentMonthOffset:(NSInteger)offset {
    
    NSDateComponents *c = [self YMDComponents];
    c.month = offset;
    c.year = 0;
    c.day = 0;
    NSDate *d = [[NSCalendar currentCalendar] dateByAddingComponents:c toDate:self options:0];
    return [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:d].length;
}
// 相对于当前月偏移量为offset的总星期数
- (NSUInteger)numberOfWeeksInCurrentMonthOffset:(NSInteger)offset {
    
    NSDateComponents *c = [self YMDComponents];
    c.month = offset;
    c.year = 0;
    c.day = 0;
    NSDate *d = [[NSCalendar currentCalendar] dateByAddingComponents:c toDate:self options:0];
    
    NSDate *startDate = nil;
    BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMonth startDate:&startDate interval:NULL forDate:d];
    NSAssert1(ok, @"numberOfWeeksInCurrentMonthOffset = %@", self);
    NSUInteger firstWeekDay = [startDate weeklyOrdinality];
    NSUInteger monthDays = [d numberOfDaysInCurrentMonth];
    NSUInteger weeks = 0;
    if (firstWeekDay > 1) {
        weeks += 1;
        monthDays -= (7 - firstWeekDay + 1);
    }
    weeks += monthDays / 7;
    weeks += (monthDays % 7 > 0) ? 1: 0;
    
    return weeks;
    
}







@end
